package com.yowasa.xiuxian.engine.message.user;


import com.yowasa.xiuxian.annoations.CMD;
import com.yowasa.xiuxian.constant.XiuxianConstants;
import com.yowasa.xiuxian.engine.HoneEngine;
import com.yowasa.xiuxian.engine.hone.MentalInterface;
import com.yowasa.xiuxian.engine.message.CmdMessageHandler;
import com.yowasa.xiuxian.entity.bo.user.UserAttr;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.bo.user.UserBattle;
import com.yowasa.xiuxian.entity.mys.SendMessage;
import com.yowasa.xiuxian.exception.HandlerException;
import com.yowasa.xiuxian.service.helper.MessageHelper;
import com.yowasa.xiuxian.service.helper.UserHelper;
import com.yowasa.xiuxian.service.helper.UserTagHelper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

/**
 * 处理新建用户
 */

@CMD(name = "/转生", outCmd = true)
public class RetryUserHandler extends CmdMessageHandler {
    @Autowired
    private UserHelper userHelper;
    @Autowired
    private UserTagHelper userTagHelper;
    @Autowired
    private MessageHelper messageHelper;

    @Override
    public void execute(SendMessage message) {

        UserBO userBO = userHelper.checkOpUser(message);
        int value = userTagHelper.getValue(userBO.getUserId(), XiuxianConstants.TAG_FEISHENG);
        if (userBO.getStage() !=9 || userBO.getLevel()!=101 || value ==0){
            throw new HandlerException("请先飞升再来轮回");
        }
        if (value >=9){
            messageHelper.replyMys(message, "你已经飞升了9次了，已达轮回极境，你仍可继续转生提升基础属性，但九转轮回功法无法再叠加倍率");
        }
//        resourceHelper.lockResource(userBO.getGuildId(), this.getClass().getName(), 100 * 360);
        MentalInterface mentalInterface = HoneEngine.mentalMap.get("九转轮回功");
        userBO.getUserConfig().setMental("九转轮回功");
        if (!userBO.getUserConfig().getCanStudyStrategies().contains(mentalInterface.bsList().get(0))) {
            userBO.getUserConfig().getCanStudyStrategies().addAll(mentalInterface.bsList());
        }
        // 打回初始状态
        userBO.setStage(1);
        userBO.setLevel(0);
        userBO.setStartAt(new Date());
        userBO.setGuildId(message.getVilla_id());
        UserAttr userAttr = userBO.getUserAttr();
        userAttr.setCon(Math.min(userAttr.getCon() + 3, 100));
        userAttr.setDex(Math.min(userAttr.getDex() + 3, 100));
        userAttr.setInts(Math.min(userAttr.getInts() + 3, 100));
        userAttr.setLuc(Math.min(userAttr.getLuc() + 3, 100));
        userAttr.setStr(Math.min(userAttr.getStr() + 3, 100));
        userAttr.setUnd(Math.min(userAttr.getUnd() + 3, 100));

        userBO.setUserOriginBattle(UserBattle.genBattleAttr(userAttr));
        userBO.setUserBattle(userBO.getUserOriginBattle());
        userTagHelper.setValue(userBO.getUserId(), XiuxianConstants.BAIDUAN_TIMES,0);
        userHelper.saveUserInfo(userBO);
        userHelper.lockUser(userBO,100,userBO.getName()+"正在轮回转生");
        messageHelper.replyMys(message, "你已成功转生");
    }


    public String desc(){
        return new StringBuffer().append("修炼到尘世执政官圆满才可以转生，转生后会获得基础属性提升与专属心法\n")
                .toString();

    }

}
